package com.witframework.web.util;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import com.witframework.xml.XMLDocument;

/**
 *
 * <p>Title: 下载文件的辅助类.</p>
 * <p>Description: 调用servlet:DownLoadFile.
 * <pre>
 * 如果是数据库中文件，您需要使用以下方法：
 * 1.setTableName()
 * 2.setBlobColumn()
 * 3.setCondition()
 * 4.setFileName()
 * 5.setMimeType()
 * 6.setIsShowInExperlor()
 *
 * <br>
 * 如果是显示本地文件，您需要使用以下方法：
 * 1.setFileName()
 * 2.setMimeType()
 * 3.setFileName()
 * 4.setIsShowInExperlor()
 * <br>
 * <br>
 * 用法：
 * 1.下载服务器上的文件
 * &%@ page import="com.sinosoftgroup.msd.util.DownLoadFileHelper"%>
 * &%
 * DownLoadFileHelper d = new DownLoadFileHelper();
 *
 * d.setMimeType(DownLoadFileHelper.MimeType_EXCEL);
 * d.setFileName("f:\\工作表.xls");
 * %>
 * &br>
 * &a href="&%=d.getLink(request)%&"&f:\\工作表.xls&/a&
 *
 * <br>
 * 2.下载数据库中的文件
 * &%@ page import="com.sinosoftgroup.msd.util.DownLoadFileHelper"%&
 * &%
 * DownLoadFileHelper d = new DownLoadFileHelper();
 *
 * //下载数据库中的文件
 * d.setMimeType(DownLoadFileHelper.MimeType_WORD);
 * d.setIsShowInExperlor(true);
 * d.setTableName("TC_SYS_REQ_APPEND");
 * d.setBlobColumn("appendphoto");
 * d.setFileName("ck.doc");
 * d.setCondition("appendid=327");
 * %&
 * &br&
 * &a href="&%=d.getLink(request)%>"&asdfsadf&/a&
 *
 * <br>
 * 3.显示图像
 * &%@ page import="com.sinosoftgroup.msd.util.DownLoadFileHelper"%&
 * &%
 * DownLoadFileHelper d = new DownLoadFileHelper();
 *
 * //下载数据库中的图像
 * d.setMimeType(DownLoadFileHelper.MimeType_JPEG);
 * d.setIsShowInExperlor(true);
 * d.setTableName("TC_SYS_REQ_APPEND");
 * d.setBlobColumn("appendphoto");
 * d.setFileName("fuck.doc");
 * d.setCondition("appendid=327");
 * %&
 * &br>
 * &img src="&%=d.getLink(request)%&"&
 * &br&
 *
 * @version 1.0
 */
public class DownLoadFileHelper { 
	String memetypes="<mimetypes>" +
 		"    <mime extension=\"pdf\">application/pdf</mime>" +
 		"    <mime extension=\"pdf\">application/pdf</mime>" +
 		"    <mime extension=\"doc\">application/msword</mime>" +
 		"    <mime extension=\"sxw\">application/vnd.sun.xml.writer</mime>" +
 		"    <mime extension=\"stw\">application/vnd.sun.xml.writer.template</mime>" +
 		"    <mime extension=\"sxg\">application/vnd.sun.xml.writer.global</mime>" +
 		"    <mime extension=\"sxc\">application/vnd.sun.xml.calc</mime>" +
 		"    <mime extension=\"stc\">application/vnd.sun.xml.calc.template</mime>" +
 		"    <mime extension=\"sxi\">application/vnd.sun.xml.impress</mime>" +
 		"    <mime extension=\"sti\">application/vnd.sun.xml.impress.template</mime>" +
 		"    <mime extension=\"sxd\">application/vnd.sun.xml.draw</mime>" +
 		"    <mime extension=\"std\">application/vnd.sun.xml.draw.template</mime>" +
 		"    <mime extension=\"sxm\">application/vnd.sun.xml.math</mime>" +
 		"    <mime extension=\"kwd\">application/x-kword</mime>" +
 		"    <mime extension=\"ksp\">application/x-kspread</mime>" +
 		"    <mime extension=\"kpr\">application/x-kpresenter</mime>" +
 		"    <mime extension=\"rtf\">text/rtf</mime>" +
 		"    <mime extension=\"ps\">application/postscript</mime>" +
 		"    <mime extension=\"xls\">application/msexcel</mime>" +
 		"    <mime extension=\"ppt\">application/mspowerpoint</mime>" +
 		"    <mime extension=\"pps\">application/mspowerpoint</mime>" +
 		"    <mime extension=\"html\">text/html</mime>" +
 		"    <mime extension=\"htm\">text/html</mime>" +
 		"    <mime extension=\"txt\">text/plain</mime>" +
 		"    <mime extension=\"dbf\">application/msexcel</mime>" +
 		"    <mime extension=\"jpg\">image/jpeg</mime>" +
 		"    <mime extension=\"jpeg\">image/jpeg</mime>" +
 		"    <mime extension=\"png\">image/png</mime>" +
 		"    <mime extension=\"gif\">image/gif</mime>" +
 		"    <mime extension=\"tif\">image/tiff</mime>" +
 		"    <mime extension=\"tiff\">image/tiff</mime>" +
 		"</mimetypes>";

  private String blobColumn = null;
  private String tableName = null;
  private String appendCondition = null;
  private String mimeType = null;
  private String fileName = null;
  private String charset = null;
  private boolean isShowInExperlor = false;

  /**
   * 根据文件名获得MIME类型.
   * @param fileName 文件名
   * @return MIME类型　
   */
  public String getMimeTypeFromFileName(String fileName) {
    String type = "application/x-msdownload";
    if (null == fileName) {
      return null;
    }
    int pos = fileName.lastIndexOf(".");
    String extName = fileName.substring(pos + 1, fileName.length());
    java.io.ByteArrayInputStream is=new java.io.ByteArrayInputStream(memetypes.getBytes());
    try {
		XMLDocument xml=new XMLDocument(is);
		type=xml.findSingleNodeText("//mimetypes/mime[@extension=\""+extName+"\"]");
	} catch (Exception e) {		
		e.printStackTrace();
	} 
    return type;
  }

  public String getLink(HttpServletRequest request) throws UnsupportedEncodingException {
    StringBuffer link = new StringBuffer();
    StringBuffer condition = new StringBuffer();
    link.append(request.getContextPath());
    link.append("/downloadfile?");
    if (this.blobColumn != null) {
      link.append("&blobColumn=" + this.blobColumn);
    }
    if (this.tableName != null) {
      link.append("&tableName=" + this.tableName);
    }
    if (this.mimeType != null) {
      link.append("&mimeType=" + this.mimeType);
    }
    else {
      String mineTypeTemp = getMimeTypeFromFileName(this.fileName);
      if (null != mineTypeTemp) {
        link.append("&mimeType=" + mineTypeTemp);
      }
    }
    if (this.fileName != null) {
      link.append("&fileName=" +this.fileName);
    }
    if (this.charset != null) {
      link.append("&charset=" + this.charset);
    }
    link.append("&isShowInExperlor=" + String.valueOf(this.isShowInExperlor));

    if (null != this.appendCondition) {
      condition.append(this.appendCondition);
    }
    if (condition.length() > 0) {
      link.append("&condition="
                  + URLEncoder.encode(condition.toString(), "UTF-8"));
    }

    return link.toString();
  }

  public void setFileName(String fileName) {
    this.fileName = fileName;
  }

  public void setCharset(String charset) {
    this.charset = charset;
  }

  public void setAppendCondition(String appendCondition) {
    this.appendCondition = appendCondition;
  }

  public boolean isIsShowInExperlor() {
    return isShowInExperlor;
  }

  public void setIsShowInExperlor(boolean isShowInExperlor) {
    this.isShowInExperlor = isShowInExperlor;
  }

  public void setMimeType(String mimeType) {
    this.mimeType = mimeType;
  }

  public void setTableName(String tableName) {
    this.tableName = tableName;
  }

  public void setBlobColumn(String blobColumn) {
    this.blobColumn = blobColumn;
  }


  public void reset() {
    this.blobColumn = null;
    this.tableName = null;
    this.appendCondition = null;
    this.mimeType = null;
    this.fileName = null;
    this.charset = null;
    this.isShowInExperlor = false;
  }
}

